package com.dsjy.yf.address.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsjy.yf.address.config.base.BizException;
import com.dsjy.yf.address.config.base.CommonEnum;
import com.dsjy.yf.address.entity.dataobject.TownDO;
import com.dsjy.yf.address.entity.myenum.OperateEnum;
import com.dsjy.yf.address.mapper.TownMapper;
import com.dsjy.yf.address.service.TownService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author whj
 * @version 1.0
 * @date 2021-01-14 15:08
 */
@Service
public class TownServiceImpl implements TownService {
    @Resource
    private TownMapper townMapper;


    @Override
    public Integer serchMaxTownCode(Integer parentCode) {
        Integer maxCode = townMapper.serchMaxTownCode(parentCode);
        if (maxCode == null) {
            return parentCode * 1000;
        }
        return maxCode;
    }

    @Override
    @Cacheable(value = "town", key = "getMethodName()")
    public List<TownDO> searchAll() {
        return townMapper.searchAll();
    }

    @Override
    @Cacheable(value = "town", key = "getMethodName()+#parentCode")
    public List<TownDO> searchByCountyCode(Integer parentCode) {
        return townMapper.searchByCountyCode(parentCode);
    }

    @Override
    @Cacheable(value = "town", key = "getMethodName()+#townCode")
    public String searchByTownCode(Integer townCode) {
        return townMapper.searchByTownCode(townCode);
    }

    @Override
    @CacheEvict(value = "town", allEntries = true)
    @Transactional(rollbackFor = RuntimeException.class)
    public Integer addUpdDel(String operate, TownDO townDO) {
        Integer result = null;
        if (OperateEnum.ADD.getOperate().equals(operate) && townDO.getTownName() != null) {
            result = addTown(townDO);
        } else if (OperateEnum.UPDATE.getOperate().equals(operate) && townDO.getId() != null) {
            result = updTown(townDO);
        }
        return result;
    }

    @Override
    public Integer addTown(TownDO townDO) {
        if (townDO.getParentCode() == null || String.valueOf(townDO.getParentCode()).length() != 6) {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
        Integer maxTownCode = serchMaxTownCode(townDO.getParentCode());
        townDO.setCreateTime(new Timestamp(System.currentTimeMillis()));
        if (maxTownCode != null) {
            townDO.setTownCode(++maxTownCode);
            townDO.setSonCode(maxTownCode % 1000);
            return townMapper.addTown(townDO);
        }
        return null;
    }


    @Override
    public Integer updTown(TownDO townDO) {
        townDO.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        return townMapper.updTown(townDO);
    }

    @Override
    public IPage searchWithPage(Integer pageNum, Integer pageSize, QueryWrapper wrapper) {
        wrapper.setEntityClass(TownDO.class);
        Page<TownDO> page = new Page<>(pageNum, pageSize);
        return townMapper.selectPage(page, wrapper);

    }
}
